<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
  </forms>
</template>

<script>
import {ajax} from "@/js/api_tool.js";
import {iframe, in_right_admin} from "@/js/tool.js";
import s94Mjs from "s94-js";
import {_config} from "@/js/s94_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let forms_config = {
      name: {label:'角色名',name:'name',type:'text_one'},
      rights: {label:'权限选择',name:'rights',type:'checkbox',config:[],tips:''},
    }
    if (id){
      forms_config.create_time = {label:'创建时间',name:'create_time',tips:''};
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.id ? `/admin/admin_role/edit?id=`+this.id : '/admin/admin_role/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('admin_role/index', '$refs.list_url.list');
          iframe.hide('admin_role/edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    },
    encode_rights(rights_value){
      let app = this, rights = [];
      rights_value  = rights_value ? rights_value.split(',') : [];
      s94Mjs.each(app.config.rights.config, function(row, c) {
        let vals = [],row_all = true;
        s94Mjs.each(row.children, function(row){
          if (~rights_value.indexOf(row.value)){
            vals.push(row.value);
          }else {
            row_all = false;
          }
        })
        if(row_all){
          rights.push(row.value)
        }else{
          rights = rights.concat(vals);
        }
      });
      return rights.join(',');
    },
    decode_rights(){
      let app = this, rights = [];
      let value_arr = this.config.rights.value ? this.config.rights.value.split(',') : [];
      s94Mjs.eachloop(app.config.rights.config, 'children', function(row){
        if (in_right_admin(row.value, value_arr)) rights.push(row.value);
        return true;
      })
      this.config.rights.value = rights.join(',');
    },
  },
  watch: {
    'config.rights.value'(){
      // this.decode_rights()
    },
    'config.rights.config'(){
      // this.decode_rights()
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/admin_role/info?id='+this.id, cache:true}, (res)=>{
        if (res.data.is_default==1) this.btns=[];
        if (typeof res.data.rights === 'object') res.data.rights = Object.values(res.data.rights).join(',');
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('edit');
        }else {
          this.$router.push('index');
        }
      })
    }

    //其他初始化操作
    let is_tenant = _config('this_tenant.id') > 0 ? 1 : 0;
    ajax({url:'/admin/admin_role/rights?is_tenant='+is_tenant, cache:true}, (res)=>{
      this.config.rights.config = res.data;
      return false
    })
  }
}
</script>
